package com.alan.camunda.boot.delegate;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.model.bpmn.instance.FlowElement;
import org.springframework.stereotype.Component;

/**
 * FlowElement 对象在流程运行时会被缓存起来，因为在一个流程实例执行时对其设置属性，在之后都可以被访问
 */
@Component("modelInstanceCheckDelegate")
@Slf4j
public class ModelInstanceCheckDelegate implements JavaDelegate {

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        FlowElement fe = execution.getBpmnModelElementInstance();

        String alexAttr = fe.getAttributeValue("alex_attr");
        if (StringUtils.isBlank(alexAttr)) {
            log.info("alex_attr is blank, set it to default value: 123456");
            fe.setAttributeValue("alex_attr", "123456");
        } else {
            log.info("alex_attr is: {}", alexAttr);
        }

    }

}
